package com.honour.busserver.backup.controller;

import com.honour.busserver.backup.entity.QuartzBean;
import com.honour.busserver.backup.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("task")
@CrossOrigin
public class TaskController {

    @Autowired
    private Scheduler scheduler;

    @PostMapping("/createJob")
    public void createJob(QuartzBean quartzBean){
        //进行测试所以写死
        quartzBean.setJobClass("com.honour.busserver.backup.job.DmBackUpJob");
        quartzBean.setJobName("test1");
        quartzBean.setCronExpression("*/10 * * * * ?");
        QuartzUtils.createScheduleJob(scheduler,quartzBean,"");
    }


    @PostMapping ("/pauseJob")
    public String  pauseJob()  {
        try {
            QuartzUtils.pauseScheduleJob (scheduler,"test1");
        } catch (Exception e) {
            return "暂停失败";
        }
        return "暂停成功";
    }

    @PostMapping("/runOnce")
    public String  runOnce()  {
        try {
            QuartzUtils.runOnce (scheduler,"test1");
        } catch (Exception e) {
            return "运行一次失败";
        }
        return "运行一次成功";
    }

    @PostMapping("/resume")
    public String  resume()  {
        try {

            QuartzUtils.resumeScheduleJob(scheduler,"test1");
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }

    @PostMapping("/update")
    public String  update(QuartzBean quartzBean) {
        try {
            //进行测试所以写死
            quartzBean.setJobClass("com.hjljy.blog.Quartz.MyTask1");
            quartzBean.setJobName("test1");
            quartzBean.setCronExpression("*/10 * * * * ?");
            QuartzUtils.updateScheduleJob(scheduler, quartzBean,"");
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }


}
